package com.aierkeji.call.b2c.dao.jpa.custom.ipml;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.orm.hibernate5.HibernateTemplate;

import com.aierkeji.call.b2c.contants.Contants;
import com.aierkeji.call.b2c.dao.jpa.custom.AkjCallApprovalHistroDaoCustom;
import com.aierkeji.call.b2c.dao.jpa.custom.AkjCallClueDaoCustom;
import com.aierkeji.call.b2c.entity.AkjCallApprovalHistroVo;
import com.aierkeji.call.b2c.entity.AkjCallClue;
import com.aierkeji.call.b2c.entity.AkjCallClueVo;
import com.aierkeji.call.b2c.entity.AkjCallUser;
import com.aierkeji.call.b2c.entity.PageBean;
import com.aierkeji.call.b2c.entity.contants.Gender;
import com.aierkeji.call.b2c.util.DateTimeUtils;
import com.aierkeji.call.b2c.util.StringUtils;


public class AkjCallApprovalHistroDaoCustomImpl implements AkjCallApprovalHistroDaoCustom{

	@Autowired
	HibernateTemplate hibernateTemplate;
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Override
	public PageBean<AkjCallApprovalHistroVo> findApprovalByApprovalIdAndApprovalResultforPage(Map<String, Object> map) {
		
		PageBean<AkjCallApprovalHistroVo> pageBean = new PageBean<AkjCallApprovalHistroVo>();
		
		int pageSize 				= (Integer)map.get("pageSize") == null ? Contants.PAGE_SIZE : (Integer)map.get("pageSize");
		int page				 	= Integer.valueOf(map.get("page").toString());
		Long id 					= (Long)map.get("approvalId");
		
		if (page <= 0){
            throw new RuntimeException("当前页数必须大于1");
        }
        
        String countSql = "select count(1) from akj_call_approval_histroy t where t.approval_user_id = ? and t.approval_result is null" ;
        
        String sql = " SELECT t2.id, t1.account, t1.name, t2.apply_at,t2.remark FROM akj_call_user t1 JOIN "
        		+ "( SELECT t.id,t.apply_user_id, t.apply_at,t.remark FROM akj_call_approval_histroy t WHERE t.approval_user_id = ? AND t.approval_result IS NULL ) t2 "
        		+ " ON t1.id = t2.apply_user_id " ;
       
        List <Object> queryList=new  ArrayList<Object>();
		queryList.add(id);
        
        int totalSize = jdbcTemplate.queryForObject(countSql,queryList.toArray(),Integer.class);
        
        if (totalSize == 0){
        	pageBean.setData(null);
        	return pageBean;
        }
        
        //总页数
        int totalPage = totalSize%pageSize == 0 ? totalSize/pageSize : totalSize/pageSize + 1;
        //开始位置
        int offset = (page -1)*pageSize;
        //return item size
        int limit =  pageSize;
        sql += " limit "+ limit +" offset "+offset;
    	List<AkjCallApprovalHistroVo> akjCallApprovalHistroList = jdbcTemplate.query(sql, new RowMapper<AkjCallApprovalHistroVo>() {  
		    public AkjCallApprovalHistroVo mapRow(ResultSet rs, int rowNum) throws SQLException {  
		    	AkjCallApprovalHistroVo akjCallApprovalHistroVo = new AkjCallApprovalHistroVo();  
		    akjCallApprovalHistroVo.setPhone(rs.getString("account"));
		    akjCallApprovalHistroVo.setApplyUserNmae(rs.getString("name"));
		    akjCallApprovalHistroVo.setRemark(rs.getString("remark"));
		    akjCallApprovalHistroVo.setApplyAt(rs.getDate("apply_at"));
		    akjCallApprovalHistroVo.setId(rs.getLong("id"));
		    return akjCallApprovalHistroVo;  
		    }  
		  },queryList.toArray());  
    	
    	
    	pageBean.setData(akjCallApprovalHistroList);
    	pageBean.setElementTotalSize(akjCallApprovalHistroList.size());
    	pageBean.setTotalSize(totalSize);
    	pageBean.setTotalPage(totalPage);
    	pageBean.setPage(page);
    	pageBean.setSize(pageSize);
        return pageBean;

	}
	
}
